#Include "Fileio.ch"
#Include "Topconn.ch"
#Include "tbiconn.ch"

/*
+----------------------------------------------------------------------------+
!                             FICHA TECNICA DO PROGRAMA                      !
+----------------------------------------------------------------------------+
!Programa          ! CPL04DMS                                                !
+------------------+---------------------------------------------------------+
!Descricao         ! DMS                                                     !
!                  ! ImportCabinet.                                          !
!                  ! Importacao dados conservadoras                          !
!                  !                                                         !
+------------------+---------------------------------------------------------+
!Autor             ! TSC505 - Felipe Jose Limas                              !
+------------------+---------------------------------------------------------+
!Data de Criacao   ! 31/05/12                                                !
+------------------+---------------------------------------------------------+
!   ATUALIZACOES                                                             !
+-------------------------------------------+-----------+-----------+--------+
!   Descricao detalhada da atualizacao      !Nome do    ! Analista  !Data da !
!                                           !Solicitante! Respons.  !Atualiz.!
+-------------------------------------------+-----------+-----------+--------+
!                                           !           !           !        !
!                                           !           !           !        !
+-------------------------------------------+-----------+-----------+--------+
!                                           !           !           !        !
!                                           !           !           !        !
+-------------------------------------------+-----------+-----------+--------+
*/


User Function CPL04DMS()
Local nHandle  :=-1
Local cLinha   :=""
Local cQuery   :=""
Local cAli     :=GetNextAlias()
Local cArq     :=StrTran("\dms\gerados\ImportCabinet_"+DtoS(dDataBase)+Time()+".txt", ":", "")

//campos do arquivo
Local cdCabinet      :=""
Local dsCabinet      :=""
Local cdStatus       :=""
Local cdCabinetType  :=""
Local dsManufacturer :=""
Local cdModel        :=""
Local dsModel        :=""
Local dsVoltage      :=""
Local dtManufacture  :=""
nQtdR :=0

cQuery:= " SELECT T9_FILIAL, T9_CODBEM, T9_DMODCON, T9_SITBEM, T9_TIPO, T9_FABRICA, T9_VOLTS, T9_DTCOMPR, T9_CODESTO ,T9_SITMAN,T9_DTCOMPR "
cQuery+= " FROM "+RetSqlName("ST9")+" ST9 "
cQuery+= " WHERE T9_ZINCDMS = 'S' AND "
cQuery+= "       T9_PROPRI  = 'D' AND "
cQuery+= "       T9_ZALTDMS = 'S' AND "
cQuery+= "       "+RetSqlCond("ST9")

TCQuery ChangeQuery(cQuery) New Alias &(cAli)

Count to nQtdR

If nQtdR > 0
	nHandle:= FCREATE(cArq)
	If nHandle == -1
		conout("Erro ao criar arquivo - ferror " + Str(Ferror()))
	Else
		&(cAli)->(dbGoTop())
		
		Do While !&(cAli)->(EOF())
			cdCabinet      := Padr(&(cAli)->(T9_CODBEM),20)+Chr(9)
			dsCabinet      := Padr(POSICIONE("SB1",1,XFILIAL("SB1")+&(cAli)->(T9_CODESTO),"B1_DESC"),100)+Chr(9)
			cdStatus       := Padr(IIF(&(cAli)->(T9_SITMAN) == "A", "ACT", "DEACT"),5)+Chr(9)
			cdCabinetType  := Padr(IIF(&(cAli)->(T9_TIPO) == "H", "HORIZONTAL", IIF(&(cAli)->(T9_TIPO) == "V", "VERTICAL", "OUTROS")),100)+Chr(9)
			dsManufacturer := Padr(fBuscaCpo("ST7", 1, &(cAli)->(T9_FILIAL)+&(cAli)->(T9_FABRICA), "T7_NOME"),100)+Chr(9)
			cdModel        := Padr(POSICIONE("SB1",1,XFILIAL("SB1")+&(cAli)->(T9_CODESTO),"B1_DESC"),20)+Chr(9)
			dsModel        := Padr(&(cAli)->(T9_CODESTO),100)+Chr(9)
			dsVoltage      := Padr(Alltrim(IIF(&(cAli)->(T9_VOLTS) == "1", "110", IIF(&(cAli)->(T9_VOLTS) == "2", "220", "BIVOL"))),5)+Chr(9)
			dtManufacture  :=Left(&(cAli)->(T9_DTCOMPR), 4)+"-"+SubStr(&(cAli)->(T9_DTCOMPR), 5, 2)+"-"+Right(&(cAli)->(T9_DTCOMPR), 2)+Chr(9)
			
			cLinha := cdCabinet+dsCabinet+cdStatus+cdCabinetType+dsManufacturer+cdModel+dsModel+dsVoltage+dtManufacture
			FWrite(nHandle, cLinha+Chr(13)+Chr(10))
			&(cAli)->(dbSkip())
		EndDo
		
		&(cAli)->(dbCloseArea())
		FClose(nHandle)
		
		/*Tenta se conectar ao servidor ftp na porta 21
		//conexao sera feita com programa externo ws_ftp - gabriel 17/10/2012
		If !FTPCONNECT("189.59.223.136" , 21 ,"FTP_UDB_CPL", "16Access97")
			conout("Nao foi possivel conectar")
			Return .F.
		EndIf
		
		If !FTPUPLOAD(cArq, "/FTP_UDB_CPL/Inbox/"+SubStr(cArq, At("IMPORT", UPPER(cArq))))
			conout("Nao foi possivel realizar o upload!!")
			Return .F.
		EndIf
		FTPDISCONNECT() 
		*/
	EndIf
EndIf
Return cArq